package com.org.khtn.command.delete;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.google.common.collect.Multimap;
import com.org.khtn.command.MyQuery;
import com.org.khtn.command.select.Where;
import com.org.khtn.data.Data;
import com.org.khtn.data.DataTables;
import com.org.khtn.data.Manager;
import com.org.khtn.data.Table;
import com.org.khtn.ultilities.Ultilities;

public class DeleteQuery extends MyQuery{
	final String className = getClass().getSimpleName();

	public DeleteQuery(String Query, String databaseName) {
		dt = new DataTables(new Data(databaseName, "sadsa", "mytest\\data\\"));
		query = Query;
		parse = new ParseDelete(Query);
		table = dt.getAllTableName();

	}

	public static void main(String[] args) {
		DeleteQuery a = new DeleteQuery(
				"delete from sach          where       masach='18'",
				"qlhs");
		a.executeNonQuery();
	}
	/**
	 * 
	 * @return so dong nếu thành công, -10 nếu sai cú pháp -11 nếu ko có bảng
	 *         tồn tại
	 */
	
	@Override
	public int executeNonQuery() {
		// TODO Auto-generated method stub
		final String methodName = getClass().getSimpleName();
		if (!parse.eval())
			return -10;
		else
			key = parse.parse();

		if (!tableExist(key[0]))
			return -11;
		System.out.println(className + methodName + "key[1]: " + key[1]);
		Table myTable = dt.getTable(key[0]);
		Vector rowID = Ultilities.filterByWhereCondition(myTable,key[1],dt);
		if (rowID == null)
			return 0;
		int length12 = rowID.size();
		for (int i = 0; i < length12; i++) {
			dt.deleteTableRecord(key[0], (String) rowID.get(i));
		}
		return length12;

	}

	

}
